/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.sample.slice;

import com.andremion.louvre.Louvre;
import com.andremion.louvre.sample.PupUpCallBack;
import com.andremion.louvre.sample.ResourceTable;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

import static com.andremion.louvre.Louvre.EXTRA_SELECTION;
import static com.andremion.louvre.StoragePermissionActivity.RESULT_OK;

/**
 * MainAbilitySlice Class
 */
public class MainAbilitySlice extends Ability implements PupUpCallBack {
    public static Ability mainAbilityContext = null;
    private TableLayoutManager mLayoutManager;
    private ItemProvider itemProvider;
    int maxLimit = 0;
    String[] selectedIMageType = {};


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        mainAbilityContext = this;
        itemProvider = new ItemProvider(getContext(), new ArrayList<>(), new ArrayList<>());
        Component addImageButtonComp = findComponentById(ResourceTable.Id_add_images);
        mLayoutManager = new TableLayoutManager();
        mLayoutManager.setColumnCount(4);
        if (addImageButtonComp instanceof Image) {
            Image addImageButton = (Image) addImageButtonComp;
            addImageButton.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Picker numberPicker = new Picker(getContext());
                    numberPicker.setMinValue(1);
                    numberPicker.setMaxValue(100);
                    numberPicker.setNormalTextSize(50);
                    numberPicker.setSelectedTextColor(Color.GREEN);
                    numberPicker.setSelectedTextSize(60);
                    numberPicker.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
                    numberPicker.setAlignment(LayoutAlignment.CENTER);
                    ShapeElement shape = new ShapeElement();
                    shape.setShape(ShapeElement.RECTANGLE);
                    shape.setRgbColor(RgbColor.fromArgbInt(0xFF40E0D0));
                    numberPicker.setDisplayedLinesElements(shape, shape);
                    ChooseImageLimitDialog chooseImageLimitDialog = new ChooseImageLimitDialog.Builder(getContext())
                            .title(ResourceTable.String_max_select_prompt)
                            .setPickerView(numberPicker)
                            .show();
                    callDialog(chooseImageLimitDialog);
                }
            });

            Component imageListComp = findComponentById(ResourceTable.Id_image_list_container);
            if (imageListComp instanceof ListContainer) {
                ListContainer imageList = (ListContainer) imageListComp;
                imageList.setLayoutManager(mLayoutManager);
                imageList.setItemProvider(itemProvider);
            }
        }
    }

    private void callDialog(ChooseImageLimitDialog chooseImageLimitDialog) {
        chooseImageLimitDialog.setPupUpCallBack(this);
        chooseImageLimitDialog.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if (resultCode == RESULT_OK) {
            List<Uri> selection = (List<Uri>) resultData.getSerializableParam(EXTRA_SELECTION);
            itemProvider.setData(selection);
        }
    }

    @Override
    public void numberPickCallBack(int maxLimit) {
        this.maxLimit = maxLimit;
        SelectImageTypeDialog mSelectImageTypeDialog = new SelectImageTypeDialog.Builder(this)
                .title(ResourceTable.String_media_type_prompt)
                .show();
        mSelectImageTypeDialog.setPupUpCallBack(this);
        mSelectImageTypeDialog.show();
    }

    @Override
    public void imageTypePickCallBack(String[] selectedIMageType) {
        this.selectedIMageType = selectedIMageType;
        Louvre.init(mainAbilityContext)
                .setRequestCode(111)
                .setMaxSelection(maxLimit)
                .setSelection(itemProvider.getmUris())
//                .setThemeColor(ResourceTable.Color_theme_color)
                .setMediaTypeFilter(selectedIMageType)
                .open();
    }
}